
#include "Sdr.h"
#include "Sensor_Threshold.h"

uint8 Sensor_GetReadingEvent(FullSensorRecord* pFullSensorRecord, uint8 newValue)
{
	uint8 nRetEvent = 0;
	if(newValue >= pFullSensorRecord->m_UpperNonRecoverable)
	{
		nRetEvent = AT_ABOVE_UNR;
	}
	else if(newValue >= pFullSensorRecord->m_UpperCiritcal)
	{
		nRetEvent = AT_ABOVE_UC;
	}
	else if(newValue >= pFullSensorRecord->m_UpperNonCiritcal)
	{
		nRetEvent = AT_ABOVE_UNC;
	}
	else if(newValue > pFullSensorRecord->m_LowerNonCiritcal)
	{
		nRetEvent = AT_NORMAL;
	}
	else if(newValue > pFullSensorRecord->m_LowerCiritcal)
	{
		nRetEvent = AT_BELOW_LNC;
	}
	else if(newValue > pFullSensorRecord->m_LowerNonRecoverable)
	{
		nRetEvent = AT_BELOW_LC;
	}
	else
	{
		nRetEvent = AT_BELOW_LNR;
	}
	return nRetEvent;
}

uint32 Sensor_GetGoLowOffset(FullSensorRecord* pFullSensorRecord, uint8 newValue, uint8 olderValue)
{
	uint32 nRetEvent = 0;
	SensorEvent* pEvent = (SensorEvent*)&nRetEvent;
	pEvent->m_isValid = 0;
	
	if(newValue > pFullSensorRecord->m_UpperNonRecoverable - pFullSensorRecord->m_NeHysteresis)
	{
	}
	else if(newValue > pFullSensorRecord->m_UpperCiritcal - pFullSensorRecord->m_NeHysteresis)
	{
		if(pFullSensorRecord->m_UpperThresholdMask & GH_UNR_MASK)
		{
			if(olderValue >= pFullSensorRecord->m_UpperNonRecoverable)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 1;
				pEvent->m_offset = UPPER_NON_RECOVERABLE_H;
				pEvent->m_Threshold = pFullSensorRecord->m_UpperNonRecoverable;
			}
		}
	}
	else if(newValue > pFullSensorRecord->m_UpperNonCiritcal - pFullSensorRecord->m_NeHysteresis)
	{
		if(pFullSensorRecord->m_UpperThresholdMask & GH_UC_MASK)
		{
			if(olderValue >= pFullSensorRecord->m_UpperCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 1;
				pEvent->m_offset = UPPER_CRITICAL_H;
				pEvent->m_Threshold = pFullSensorRecord->m_UpperCiritcal;
			}
		}
	}
	else if(newValue > pFullSensorRecord->m_LowerNonCiritcal)
	{
		if(pFullSensorRecord->m_UpperThresholdMask & GH_UNC_MASK)
		{
			if(olderValue >= pFullSensorRecord->m_UpperNonCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 1;
				pEvent->m_offset = UPPER_NO_CRITICAL_H;
				pEvent->m_Threshold = pFullSensorRecord->m_UpperNonCiritcal;
			}
		}
	}
	else if(newValue > pFullSensorRecord->m_LowerCiritcal)
	{
		if(pFullSensorRecord->m_LowerThresholdMask & GL_LNC_MASK)
		{
			if(olderValue >= pFullSensorRecord->m_LowerNonCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 0;
				pEvent->m_offset = LOWER_NO_CRITICAL_L;
				pEvent->m_Threshold = pFullSensorRecord->m_LowerNonCiritcal;
			}
		}
	}
	else if(newValue > pFullSensorRecord->m_LowerNonRecoverable)
	{
		if(pFullSensorRecord->m_LowerThresholdMask & GL_LC_MASK)
		{
			if(olderValue >= pFullSensorRecord->m_LowerCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 0;
				pEvent->m_offset = LOWER_CRITICAL_L;
				pEvent->m_Threshold = pFullSensorRecord->m_LowerCiritcal;
			}
		}
	}
	else
	{
		if(pFullSensorRecord->m_LowerThresholdMask & GL_LNR_MASK)
		{
			if(olderValue >= pFullSensorRecord->m_LowerNonRecoverable)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 0;
				pEvent->m_offset = LOWER_NON_RECOVERABLE_L;
				pEvent->m_Threshold = pFullSensorRecord->m_LowerNonRecoverable;
			}
		}
	}

	return nRetEvent;
}

uint32 Sensor_GetGoUpOffset(FullSensorRecord* pFullSensorRecord, uint8 newValue, uint8 olderValue)
{
	uint32 nRetEvent = 0;
	SensorEvent* pEvent = (SensorEvent*)&nRetEvent;
	pEvent->m_isValid = 0;
	
	if(newValue >= pFullSensorRecord->m_UpperNonRecoverable)
	{
		if(pFullSensorRecord->m_UpperThresholdMask & GH_UNR_MASK)
		{
			if(olderValue < pFullSensorRecord->m_UpperNonRecoverable)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 0;
				pEvent->m_offset = UPPER_NON_RECOVERABLE_H;
				pEvent->m_Threshold = pFullSensorRecord->m_UpperNonRecoverable;
			}
		}
	}
	else if(newValue >= pFullSensorRecord->m_UpperCiritcal)
	{
		if(pFullSensorRecord->m_UpperThresholdMask & GH_UC_MASK)
		{
			if(olderValue < pFullSensorRecord->m_UpperCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 0;
				pEvent->m_offset = UPPER_CRITICAL_H;
				pEvent->m_Threshold = pFullSensorRecord->m_UpperCiritcal;
			}
		}
	}
	else if(newValue >= pFullSensorRecord->m_UpperNonCiritcal)
	{
		if(pFullSensorRecord->m_UpperThresholdMask & GH_UC_MASK)
		{
			if(olderValue < pFullSensorRecord->m_UpperNonCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 0;
				pEvent->m_offset = UPPER_NO_CRITICAL_H;
				pEvent->m_Threshold = pFullSensorRecord->m_UpperNonCiritcal;
			}
		}
	}
	else if(newValue >= pFullSensorRecord->m_LowerNonCiritcal + pFullSensorRecord->m_PoHysteresis)
	{
		if(pFullSensorRecord->m_LowerThresholdMask & GL_LNC_MASK)
		{
			if(olderValue <= pFullSensorRecord->m_LowerNonCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 1;
				pEvent->m_offset = LOWER_NO_CRITICAL_L;
				pEvent->m_Threshold = pFullSensorRecord->m_LowerNonCiritcal;
			}
		}
	}
	else if(newValue >= pFullSensorRecord->m_LowerCiritcal + pFullSensorRecord->m_PoHysteresis)
	{
		if(pFullSensorRecord->m_LowerThresholdMask & GL_LC_MASK)
		{
			if(olderValue <= pFullSensorRecord->m_LowerCiritcal)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 1;
				pEvent->m_offset = LOWER_CRITICAL_L;
				pEvent->m_Threshold = pFullSensorRecord->m_LowerCiritcal;
			}
		}
	}
	else if(newValue >= pFullSensorRecord->m_LowerNonRecoverable + pFullSensorRecord->m_PoHysteresis)
	{
		if(pFullSensorRecord->m_LowerThresholdMask & GL_LNR_MASK)
		{
			if(olderValue <= pFullSensorRecord->m_LowerNonRecoverable)
			{
				pEvent->m_isValid = 1;
				pEvent->m_isDeassert = 1;
				pEvent->m_offset = LOWER_NON_RECOVERABLE_L;
				pEvent->m_Threshold = pFullSensorRecord->m_LowerNonRecoverable;
			}
		}
	}

	return nRetEvent;
}

uint32 Sensor_GetEvent(Sensor* pSensor)
{
	FullSensorRecord* pFullSensorRecord = (FullSensorRecord*)pSensor->m_pRecodHeader;

	if(pSensor->m_RawValue == pSensor->m_ReceiverValue)
	{
		return pSensor->m_EventData.m_EventData1.m_Offset;
	}
	else if(pSensor->m_RawValue < pSensor->m_ReceiverValue)
	{
		return Sensor_GetGoLowOffset(pFullSensorRecord, pSensor->m_RawValue, pSensor->m_ReceiverValue);
	}
	else
	{
		return Sensor_GetGoUpOffset(pFullSensorRecord, pSensor->m_RawValue, pSensor->m_ReceiverValue);
	}
}

void Sensor_ThresholdGetEventData(Sensor* pSensor)
{
	FullSensorRecord* pFullSensorRecord = (FullSensorRecord*)pSensor->m_pRecodHeader;
	uint32 nEvent = Sensor_GetEvent(pSensor);
	SensorEvent* pSensorEvent = (SensorEvent*) & nEvent;
	EventData* pEventData = &pSensor->m_EventData;

	pSensor->m_ReadingEvent = Sensor_GetReadingEvent((FullSensorRecord*)pSensor->m_pRecodHeader, pSensor->m_RawValue);

	if(!pSensorEvent->m_isValid) 
	{
		memset(pEventData,0,sizeof(EventData));
		return;
	}
	
    pEventData->m_EventDir  	= pSensorEvent->m_isDeassert;
    pEventData->m_EventType 	= pFullSensorRecord->m_EventType;

    pEventData->m_EventData1.m_Offset 		= pSensorEvent->m_offset;
    pEventData->m_EventData1.m_Data2Format 	= 1;
    pEventData->m_EventData1.m_Data3Format 	= 1;
    pEventData->m_EventData2	= pSensor->m_RawValue;
    pEventData->m_EventData3	= pSensorEvent->m_Threshold;
}

